package sivin.uikit.rv.lm

import android.content.Context
import android.graphics.PointF
import android.graphics.Rect
import android.util.AttributeSet
import android.util.SparseArray
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.min

class GalleryLayoutManager(val mOrientation: Int = RecyclerView.HORIZONTAL) :
    RecyclerView.LayoutManager(), RecyclerView.SmoothScroller.ScrollVectorProvider {

    private var mInitialSelectedPosition: Int = 0

    private var mCurSelectedPosition: Int = -1

    private var mFirstVisiblePosition: Int = 0

    private var mLastVisiblePos = 0;

    private val mState by lazy { State() }

    override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
        return if (mOrientation == RecyclerView.VERTICAL) {
            LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        } else {
            LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        }
    }

    override fun generateLayoutParams(c: Context?, attrs: AttributeSet?)
            : RecyclerView.LayoutParams {
        return LayoutParams(c, attrs)
    }

    override fun generateLayoutParams(lp: ViewGroup.LayoutParams?)
            : RecyclerView.LayoutParams {
        return LayoutParams(lp)
    }

    override fun checkLayoutParams(lp: RecyclerView.LayoutParams?): Boolean {
        return lp is LayoutParams
    }


    override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
        if (itemCount == 0) {
            reset()
            detachAndScrapAttachedViews(recycler)
            return
        }
        if (state.isPreLayout) {
            return
        }
        //自上次调用 onLayoutChildren 以来数据集的结构没有发生更改
        if (state.itemCount != 0 && !state.didStructureChange()) {
            return
        }
        //自上次调用 onLayoutChildren 以来数据集的结构发生了更改
        if (childCount == 0 || state.didStructureChange()) {
            reset()
        }
        mInitialSelectedPosition = min(0.coerceAtLeast(mInitialSelectedPosition), itemCount - 1)
        detachAndScrapAttachedViews(recycler)
//        firstFillCover(recycler, state, 0)
    }

    private fun reset() {
        mState.mItemsFrames.clear()
        if (mCurSelectedPosition != -1) {
            mInitialSelectedPosition = mCurSelectedPosition
        }
        mInitialSelectedPosition = min(0.coerceAtLeast(mInitialSelectedPosition), itemCount - 1)
        mFirstVisiblePosition = mInitialSelectedPosition
    }


    override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
        return null
    }

    class LayoutParams : RecyclerView.LayoutParams {
        constructor(c: Context?, attrs: AttributeSet?) : super(c, attrs)
        constructor(width: Int, height: Int) : super(width, height)
        constructor(source: MarginLayoutParams?) : super(source)
        constructor(source: ViewGroup.LayoutParams?) : super(source)
        constructor(source: RecyclerView.LayoutParams?) : super(source)
    }

    class State {
        /**
         * Record all item view 's last position after last layout
         * 记录所有ItemView上一次的position在layout后
         */
        var mItemsFrames: SparseArray<Rect> = SparseArray()

        /**
         * RecycleView距离第一次layout滚动的距离
         */
        var mScrollDelta: Int = 0
    }

}